package repository

import (
	"context"
	"github.com/aphasia51/website/internal/domain"
	"github.com/aphasia51/website/internal/repository/dao"
)

type ExperimentalPersonnelRepository struct {
	dao *dao.ExperimentalPersonnelDAO
}

func NewExperimentPersonnelRepository(dao *dao.ExperimentalPersonnelDAO) *ExperimentalPersonnelRepository {
	return &ExperimentalPersonnelRepository{
		dao: dao,
	}
}

func (r *ExperimentalPersonnelRepository) Query(ctx context.Context, a domain.ExperimentalPersonnel) ([]domain.ExperimentalPersonnel, error) {
	experimentalPersonnel, err := r.dao.Query(ctx, dao.ExperimentalPersonnel{
		Name: a.Name,
	})

	if err != nil {
		return []domain.ExperimentalPersonnel{}, err
	}

	eps := make([]domain.ExperimentalPersonnel, 0)
	for _, v := range experimentalPersonnel {
		eps = append(eps, domain.ExperimentalPersonnel{
			Id:             v.Id,
			Name:           v.Name,
			PeopleId:       v.PeopleId,
			ClassName:      v.ClassName,
			MajorName:      v.MajorName,
			CollegeName:    v.CollegeName,
			PaperTestName:  v.PaperTestName,
			Score:          v.Score,
			IfPassed:       v.IfPassed,
			ModuleName:     v.ModuleName,
			CategoryName:   v.CategoryName,
			CertificateNum: v.CertificateNum,
		})
	}

	return eps, nil
}

func (r *ExperimentalPersonnelRepository) QueryCertificate(ctx context.Context, PeopleId string) ([]domain.Certificate, error) {
	experimentalPersonnel, err := r.dao.QueryCertificate(ctx, PeopleId)

	if err != nil {
		return []domain.Certificate{}, err
	}

	cs := make([]domain.Certificate, 0)
	for _, v := range experimentalPersonnel {
		cs = append(cs, domain.Certificate{
			Id:           v.Id,
			Name:         v.Name,
			ObtainedTime: v.ObtainedTime,
			Location:     v.Location,
			Department:   v.Department,
		})
	}

	return cs, nil
}
